﻿
using System.Security.Cryptography;
using System.Text;
namespace ImageProcessing.Utility
{
    public class Util
    {
        public static string GetMD5(string input)
        {
            string result = string.Empty;
            using (MD5 md5Hash = MD5.Create())
            {
                // Convert the input string to a byte array and compute the hash. 
                byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

                // Create a new Stringbuilder to collect the bytes 
                // and create a string.
                StringBuilder sBuilder = new StringBuilder();

                // Loop through each byte of the hashed data  
                // and format each one as a hexadecimal string. 
                for (int i = 0; i < data.Length; i++)
                {
                    sBuilder.Append(data[i].ToString("x2"));
                }

                // Return the hexadecimal string. 
                result = sBuilder.ToString();
            }
            return result;
        }

        //get the first two letter of the MD5 as the level 1 folder name, the following two letters as level 2 folder
        public static string GetFolder(string input)
        {
            string hexMD5 = GetMD5(input);
            if (hexMD5.Length == 32)
            {
                return hexMD5.Substring(0, 2) + @"\" + hexMD5.Substring(2, 2);
            }
            return string.Empty;
        }
       
    }
}